/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"
#include "Resource.h"
#include "ViewUpdateDlg.h"
#include "WinUtil.h"

ViewUpdateDlg::ViewUpdateDlg(const string& data, const string& rtfData)
: m_data(data)
, m_rtfData( rtfData) 
{
	m_richEditLibrary = ::LoadLibrary(L"Msftedit.dll");
	if (!m_richEditLibrary)
	{
		_RPT1(_CRT_WARN, "LoadLibrary for Msftedit.dll failed with: %d\n", ::GetLastError());
		return;
	}

}

ViewUpdateDlg::~ViewUpdateDlg()
{
	::DeleteObject(m_UpdateIcon);
	if (m_richEditLibrary)
		::FreeLibrary(m_richEditLibrary);
	m_richEditLibrary = NULL;
}

LRESULT ViewUpdateDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	SetWindowText(CTSTRING(AUTOUPDATE_TITLE));

	GetDlgItem(IDC_STATICDESCRIPTION).SetWindowText( Text::utf8ToWide(m_data).c_str() );
	GetDlgItem(IDOK).SetWindowText(CTSTRING(AUTOUPDATE_UPDATE_NOW_BUTTON));
	GetDlgItem(IDC_IGNOREUPDATE).SetWindowText(CTSTRING(AUTOUPDATE_UPDATE_IGNORE_BUTTON));
	GetDlgItem(IDCANCEL).SetWindowText(CTSTRING(AUTOUPDATE_UPDATE_CANCEL_BUTTON));

	WinUtil::FillRichEditFromString((HWND)GetDlgItem(IDC_RTFDESCRIPTION), Text::fromUtf8(m_rtfData));

	m_UpdateIcon =  (HICON)::LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_AUTOUPDATE), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
	SetIcon(m_UpdateIcon, FALSE);
	SetIcon(m_UpdateIcon, TRUE);


	LRESULT lResult = GetDlgItem(IDC_RTFDESCRIPTION).SendMessage( EM_GETEVENTMASK, 0, 0 );
	GetDlgItem(IDC_RTFDESCRIPTION).SendMessage(EM_SETEVENTMASK , 0, lResult | ENM_LINK );
	GetDlgItem(IDC_RTFDESCRIPTION).SendMessage(EM_AUTOURLDETECT, TRUE, 0);

	CenterWindow(GetParent());
	return TRUE;
}
LRESULT ViewUpdateDlg::OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	EndDialog(wID);
	return 0;
}

LRESULT ViewUpdateDlg::OnLink(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled)
{
	const ENLINK* pL = (const ENLINK*)pnmh;
	if (WM_LBUTTONDOWN == pL->msg) {
		TCHAR buf[_MAX_PATH];
		CRichEditCtrl(pL->nmhdr.hwndFrom).GetTextRange(pL->chrg.cpMin, pL->chrg.cpMax, buf);
		::ShellExecuteW(NULL, NULL, buf , NULL, NULL, SW_SHOWNORMAL);
	} else 
		bHandled = FALSE;
	return 0;
}

/**
* @file
* $Id: ViewUpdateDlg.cpp 7011 2011-05-16 20:17:37Z sa.stolper $
*/